import logging
import datetime
import copy
import utility
import config

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache


class MainPage(webapp.RequestHandler):
    def get(self):
        query = self.request.get('q', '')
        if query:
            payload = {}
            newfilters = []
            newgroupviews = []
            count = 0
            groups = []
            groupid = 0
            analysisid = 1   

            # get the clustered search result data            
            results = memcache.get(query+'_web')
            results2 = memcache.get(query+'_cluster')
            if results2 is None or results is None:
                results = utility.get_N_google_search_results(query, 300)
                if len(results) == 0:
                    logging.debug('error.html')
                    error = template.render('error.html', {})
                    self.response.out.write(error)
                    return
                try:
                    seal = utility.seal_query_xml(query, results)
                    results2 = utility.get_cluster_results(c2stream=seal)
                    memcache.add_multi({'_web':results,
                                        '_cluster':results2},
                                       key_prefix=query, time=config.MEMCACHETIME)
                except:
                    logging.debug('error.html')
                    error = template.render('error.html', {})
                    self.response.out.write(error)
                    return
                logging.debug('Use From Web!--' + query)
            else:
                logging.debug('Use Cache!--' + query)
            
            count = len(results)
            for resu in results2['clusters']:
                title =''              
                for des in resu['phrases']:#resu['description']:
                    title += des              
                group = {'size': len(resu['documents']), 'list': [], 'description': title, 'more': False}
                if group["size"] > config.DMORE:
                    group["more"] = True      
                newfilters.append(group)

                groupid += 1
                for item in resu['documents']:
                    index = int(item) - 1
                    summary =  results[index]['Summary']
                    if len(summary) == 0 or len(results[index]['Url']) == 0:
                        continue
                    siteinfo = {'id': index + 1, 'title': results[index]['Title'][:50], 'summary':summary,
                                'url': results[index]['Url'][:30], 'clickurl': results[index]['ClickUrl'][:500], 
                                'image': 'http://' + config.ANALYSISSERVERNAME + str(analysisid) + '.appspot.com/?url=' + results[index]['ClickUrl']}
                    newfilters[len(newfilters)-1]["list"].append(siteinfo)

            newgroupviews = copy.deepcopy(newfilters[:config.GROUPITEM])

            analysisid = 1
            havedisplayeds = [] # Remove the displayed item in orther groups
            for groupview in newgroupviews:
                groupviewcopy = groupview["list"]
                groupview["list"] = []
                for item in groupviewcopy:
                    if len(groupview["list"]) < config.DISPLAYITEM:
                        #item["summary"] = item["summary"]
                        if item["id"] in havedisplayeds:
                            continue
                        item['image'] = 'http://'+ config.ANALYSISSERVERNAME + str(analysisid) + '.appspot.com/?url=' + item['clickurl']
                        analysisid =  analysisid % config.ANALYSISSERVERS + 1
                        groupview["list"].append(item)
                        havedisplayeds.append(item["id"])

            groupmore = False
            if str(len(newfilters)) > config.GMORE:
                groupmore = True

            payload = dict( filters=newfilters, query=query, groupmore=groupmore, size=str(count), 
                            filtersize=str(len(newfilters)), groupviews=newgroupviews)
            resp = template.render('index.html', payload)
        else:
            resp = template.render('form.html', {})
            #model.UserLog(useraddr = str(self.request.remote_addr), searchhit = 1, lastvisit = datetime.datetime.now()).record()
        self.response.out.write(resp)

